


;If  Select_Mode is RST (=0), then the map will scroll normally.
;When Select_Mode is on, the player uses a cursor to select things.




#macro DrawHL(y, x)

 
 push de
 push bc
 push af
 push ix
 push hl
 LD hl,x*256+y
 LD (curRow),hl
 pop hl
 push hl
 B_CALL _DispHL
 B_CALL _getKey
 pop hl
 pop ix
 pop af
 pop bc
 pop de
 
#endmacro
	
Tile_width = 1
Map_Entry_Size = 1
MAP_MASK = $ff
PMAPWIDTH = 12
PMAPHEIGHT = 10
PMAPHIEGHT = PMAPHEIGHT
delta = savesscreen


Start:

;For displaying buildings-------------
	ld a, 0
	ld (BuildingCount), a



	ld hl,21000
	ld de,$9d95
	bcall(_InsertMem)

Objects_Address .equ 10000

MapData .equ $9D95 + 12500
TileData .equ $9D95 + 19000
	

;To optimize the drawing of structures

Store_Structure_Sprite_Addresses:

	B_CALL _Load_Sprite_Data
	B_CALL _Load_Map_Data
	B_CALL _Load_Tile_Data


	


	
	
	ld hl,2
	ld (delta),hl
	ld a,1
	ld (Num_Layers),a
	xor a
	ld h,a
	ld a,(mapdata)
	ld l,a
	ld (mapheight),hl
	ld a,(mapdata+1)
	ld l,a
	ld (Mapwidth),hl
	ld hl,0
	ld (MapY),hl
	ld (MapX),hl
	ld hl,TileData
	ld (Tileptr),hl
	ld hl,MapData+2
	ld (MAPptr),hl
	ld hl,Mapbuf1
	ld (MapbufPtr),hl
	ld hl,premap
	ld (PreMapPTR),hl
	call DrawMapFull

Loop:
	
	call updatemap
	;call DrawMapFull
	
	ld a,(MapY)
	and tile_width*8-1
	ld h,a
	ld e,PMAPWIDTH*Tile_width
	call HxE

	ld de,mapbuf1
	add hl,de


	;call safecopy


	;ld a,(MapY)
	;and tile_width*8-1
	;ld h,a
	;ld e,PMAPWIDTH*Tile_width
	;call HxE

	;ld de,mapbuf1
	;add hl,de




	
	ld de, plotsscreen
	ld bc, 768
	ldir

Draw_Structures_On_The_Map:
	
	call Draw_Structures_On_Map 
	BIT Select_Mode, (IY + asm_flag1) 
	call nz, Draw_Select_Cursor   ; If Draw_Select_Cursor
					; is equal to 1, 
					; draw the cursor rather
					; then simply scroll the map empty
Keyloop:
	ld b,0
_
	inc b
	ld a,$FF
	out (1),a
	ld a,$80
	out (1),a
	nop
	nop
	in a,(1)
	inc a
	jr nz,_
	ld a,b
	cp 2
	jr nz, _-
	RES Wait_Between_Presses, (IY + asm_flag1)

	jr Keyloop
_
	

	ld a,$FF
	out (1),a
	ld a,$FE
	out (1),a
	nop
	nop
	in a,(1)
	bit 0,a
	call z,down
	bit 1,a
	call z,left
	bit 2,a
	call z,right
	bit 3,a
	call z,up

; For 2nd Key, testing the displaying and placement of structures
	ld a,$FF
	out (1),a
	ld a, $FD
	out (1),a
	nop
	nop
	in a, (1)
	bit 1,a
	call z, AddObject


	ld a,$FF
	out (1),a
	ld a, $BF
	out (1),a
	nop
	nop
	in a, (1)

	ld a,$FF
	out (1),a
	ld a, $BF
	out (1),a
	nop
	nop
	in a, (1)
	cp $DF
	jr nz, _
	
	BIT Object_Ready_To_Add, (IY + asm_flag1)
	jr z, _
	CALL AddObjectToGame
	

_

	
	CP $7F
	jr nz, _
	ld h,0
	ld l,a
	

	
	BIT Wait_Between_Presses, (IY + asm_flag1)
	
	call z, Rotate_Select_Mode
	
;=================================================================

_
	ld a,$FF
	out (1),a
	ld a,$BF
	out (1),a
	nop
	nop
	in a,(1)
	ld hl,(delta)
	bit 0,a
	jr nz,_
	ld hl,8
_
	bit 1,a
	jr nz,_
	ld hl,6
_
	bit 2,a
	jr nz,_
	ld hl,4
_
	bit 3,a
	jr nz,_
	ld hl,2
_
	bit 4,a
	jr nz,_
	ld hl,1
_
	ld (delta),hl

	bit 5,a
	jr nz,skipspeedon
	in a,(2)
	rla
	jr nc,skipspeedon
	ld a,1
	out ($20),a
	ld a,$FF
skipspeedon:
	bit 6,a
	jr nz,skipspeedoff
	in a,(2)
	rla
	jr nc,skipspeedoff
	xor a
	out ($20),a
	ld a,$FF
skipspeedoff:

	ld a,$FF
	out (1),a
	ld a,$FD
	out (1),a
	nop
	nop
	in a,(1)
	bit 6,a
	jp nz,loop
	ld hl,$9D95
	ld de, 12000
	bcall(_DelMem)
	bcall(_JForceCmdNoChar)

down:


	BIT Wait_Between_Presses, (IY + asm_flag1)
	RET nz
	BIT Select_Mode, (IY + asm_flag1)
	JP nz, Move_Select_Cursor_Down
movemapdown:
	ld hl,(mapY)
	ld de,(delta)
	add hl,de
	push hl
		ld hl,(mapheight)
		ld de,PMAPHEIGHT-2
		or a
		sbc hl,de
		sla l
		rl h
		sla l
		rl h
		sla l
		rl h
	pop de
	call cphlde
	jr c,okaydown
	ex de,hl
okaydown:
	ld (mapY),hl
	ret
up:

	BIT Wait_Between_Presses, (IY + asm_flag1)
	RET nz
	BIT Select_Mode, (IY + asm_flag1)
	JP nz, Move_Select_Cursor_Up
movemapup:
	ld hl,(mapY)
	ld de,(delta)
	or a
	sbc hl,de
	jr nc,notzeroup
	ld hl,0
notzeroup:
	ld (mapY),hl
	ret
right:

	BIT Wait_Between_Presses, (IY + asm_flag1)
	RET nz
	BIT Select_Mode, (IY + asm_flag1)
	JP nz, Move_Select_Cursor_Right

movemapright:

	ld hl,(mapx)
	ld de,(delta)
	add hl,de
	push hl
		ld hl,(mapwidth)
		ld de,PMAPWIDTH
		or a
		sbc hl,de
		sla l
		rl h
		sla l
		rl h
		sla l
		rl h
	pop de
	ex de,hl
	call cphlde
	jr c,okayright
	ex de,hl
okayright:
	ld (mapx),hl
	ret
left:

	BIT Wait_Between_Presses, (IY + asm_flag1)
	RET nz
	BIT Select_Mode, (IY + asm_flag1)
	JP nz, Move_Select_Cursor_Left

movemapleft:

	ld hl,(mapx)
	ld de,(delta)
	or a
	sbc hl,de
	jr nc,notzeroleft
	ld hl,0
notzeroleft:
	ld (mapx),hl
	ret
CpHLDE:
	or a
	sbc hl,de
	add hl,de
	ret
	
SafeCopy:	
	di
	ld c,$10
	ld a,$80
setrow:
	in f,(c)
	jp m,setrow
	out ($10),a
	ld de,12
	ld a,$20
col:
	in f,(c)
	jp m,col
	out ($10),a
	push af
	ld b,64
row:
	ld a,(hl)
rowwait:
	in f,(c)
	jp m,rowwait
	out ($11),a
	add hl,de
	djnz row
	pop af
	dec h
	dec h
	dec h
	inc hl
	inc a
	cp $2c
	jp nz,col
	ei
	ret

PMAPBUFSIZE = PMAPWIDTH*PMAPHEIGHT*8*Tile_width*Tile_width
PREMAPBUFSIZE = (PMAPWIDTH+1)*PMAPHEIGHT
PreMap = $9d95
Mapbuf1 = $9D95+2500
MapperRam = savesscreen+2

#include "Graymapper.inc"

;.fill MapperRamSize,0
#comment
;Spritesheet - test
test:
;1. blank
 .db $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00
;2. wall
 .db $FF,$FF,$80,$01,$80,$01,$80,$01,$80,$01,$80,$01,$80,$01,$80,$01,$80,$01,$80,$01,$80,$01,$80,$01,$80,$01,$80,$01,$80,$01,$FF,$FF
;3. brick
 .db $FF,$FF,$FF,$FF,$C0,$C0,$E0,$E0,$FF,$FF,$FF,$FF,$0C,$0C,$0E,$0E,$FF,$FF,$FF,$FF,$C0,$C0,$E0,$E0,$FF,$FF,$FF,$FF,$0C,$0C,$0E,$0E

;MapCollection - testmap
testmap:
;1. testmap
 .db 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,2,2,2,2,2,2,0,0,0,0,2,2,2,1,1,0,2,0,0,0,0,2,2,2,0,0,2,0,0,1,1,0,2,2,0,0,0,0,0,2,2,0,2,0,0,1,1,0,0,2,2,0,0,0,0,0,2,2,2,0,0,1,1,0,0,0,2,2,1,1,1,1,2,2,0,0,0,1,1,0,0,0,2,2,1,0,0,1,0,2,2,0,0,1,1,0,0,0,2,0,1,0,0,1,0,0,2,0,0,1,1,2,2,2,2,0,1,1,1,1,0,0,2,0,0,1,1,0,0,0,2,0,0,0,0,2,0,0,2,2,0,1,1,0,2,0,2,2,2,0,0,2,0,0,2,2,0,1,1,0,2,2,2,0,0,2,2,2,0,0,2,0,0,1,1,0,0,0,0,0,0,0,2,2,0,0,2,2,2,1,1,0,0,0,0,0,0,0,0,0,2,2,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1



.end



;#comment

apdbuf = savesscreen
offx = appbackupscreen
offy = offx+1
mapwidth = offy+1
mapheight = mapwidth+1
blanktile = mapheight+1
timer = blanktile+1
frame = timer+1
scrolling = frame+1


Start:
	call setupinterrupt
	ld hl,mapdata
	ld a,(hl) \ inc hl
	ld (mapheight),a
	ld a,(hl) \ inc hl
	ld (mapwidth),a
	xor a
	ld (blanktile),a
MainLoop:
	call drawmapsq
	call fastcopy	
	
	ld a,$FE
	out (1),a
	in a,(1)
	bit 0,a
	call z,Down
	bit 3,a
	call z,Up
	bit 1,a
	call z,Left
	bit 2,a
	call z,Right
	ld a,$FD
	out (1),a
	in a,(1)
	bit 2,a					;clear
	jr z,Quit
	ld a,$BF
	out (1),a
	in a,(1)
	bit 2,a					;mode
	jr z,Quit
	jr MainLoop
Quit:
	bcall(_JForceCmdNoChar)
	
SetupInterrupt:
	di
	ld a,$8B
	ld i,a
	ld hl,$8B00
	ld de,$8B01
	ld bc,256
	ld (hl),$8A
	ldir
	
	ld hl,intstart
	ld de,$8A8A
	ld bc,intend-intstart
	ldir
	
	ld a,%00000110
	out (4),a
	im 2
	ei
	ret
	
IntStart:
	ex af,af'
	ld a,(timer)
	inc a
	cp 110
	jr c,_
	xor a
_
	ld (timer),a
	srl a
	srl a
	ld (frame),a
	ex af,af'
	ret
IntEnd

Down:
	push af
		ld hl,offy
		inc (hl)		
		ld b,3
		call drawrow
		ld hl,plotsscreen+48
		ld de,plotsscreen
		ld bc,720
		ldir
		ld hl,apdbuf
		ld de,plotsscreen+(60*12)
		ld bc,4*12
		ldir
		call fastcopy

		ld hl,plotsscreen+48
		ld de,plotsscreen
		ld bc,720
		ldir
		ld hl,apdbuf+(12*4)
		ld de,plotsscreen+(60*12)
		ld bc,4*12
		ldir
		call fastcopy
		
		ld hl,plotsscreen+48
		ld de,plotsscreen
		ld bc,720
		ldir
		ld hl,apdbuf+(12*8)
		ld de,plotsscreen+(60*12)
		ld bc,4*12
		ldir
		call fastcopy

		ld hl,plotsscreen+48
		ld de,plotsscreen
		ld bc,720
		ldir
		ld hl,apdbuf+(12*12)
		ld de,plotsscreen+(60*12)
		ld bc,4*12
		ldir
	pop af
	ret
Up:
	push af
		ld hl,offy
		dec (hl)
		ld b,0
		call drawrow
		
		ld hl,plotsscreen+719
		ld de,plotsscreen+767
		ld bc,720
		lddr
		ld hl,apdbuf+(cols*12)
		ld de,plotsscreen
		ld bc,4*12
		ldir
		call fastcopy
		
		ld hl,plotsscreen+719
		ld de,plotsscreen+767
		ld bc,720
		lddr
		ld hl,apdbuf+(8*12)
		ld de,plotsscreen
		ld bc,4*12
		ldir
		call fastcopy
	
		ld hl,plotsscreen+719
		ld de,plotsscreen+767
		ld bc,720
		lddr
		ld hl,apdbuf+(4*12)
		ld de,plotsscreen
		ld bc,4*12
		ldir
		call fastcopy
		
		ld hl,plotsscreen+719
		ld de,plotsscreen+767
		ld bc,720
		lddr
		ld hl,apdbuf
		ld de,plotsscreen
		ld bc,4*12
		ldir
	pop af
	ret
	
Right:
	push af						;save the keypress
		ld hl,offx
		inc (hl)
		ld c,5
		call Drawcol
		
		ld hl,plotsscreen+768
		ld b,64
		ld de,apdbuf+128			;1 row past the bottom
		call shl
		
		call fastcopy
		ld de,apdbuf+128
		ld hl,plotsscreen+768
		ld b,64
		call shl
		
		ld hl,plotsscreen+768
		ld b,64
		ld de,apdbuf+64
		call shl
		
		call fastcopy
		ld de,apdbuf+64
		ld hl,plotsscreen+768
		ld b,64
		call shl
	
	pop af
	ret
	
shl
	ex de,hl
	dec hl
	xor a
	rld
	ex de,hl
	dec hl \ rld
	dec hl \ rld
	dec hl \ rld
	dec hl \ rld
	dec hl \ rld
	dec hl \ rld
	dec hl \ rld
	dec hl \ rld
	dec hl \ rld
	dec hl \ rld
	dec hl \ rld
	dec hl \ rld
	djnz shl
	ret
	
Left:
	push af
		ld hl,offx
		dec (hl)
		ld c,0
		call drawcol
		
		ld hl,plotsscreen-1
		ld b,64
		ld de,apdbuf
		call shr
		
		call fastcopy
		ld de,apdbuf
		ld hl,plotsscreen-1
		ld b,64
		call shr
		
		ld hl,plotsscreen-1
		ld b,64
		ld de,apdbuf+64
		call shr
		
		call fastcopy
		ld de,apdbuf+64
		ld hl,plotsscreen-1
		ld b,64
		call shr
	
	pop af
	ret
	

shr
	ex de,hl
	xor a
	rrd	\ inc hl
	ex de,hl
	inc hl \ rrd
	inc hl \ rrd
	inc hl \ rrd
	inc hl \ rrd
	inc hl \ rrd
	inc hl \ rrd
	inc hl \ rrd
	inc hl \ rrd
	inc hl \ rrd
	inc hl \ rrd
	inc hl \ rrd
	inc hl \ rrd
	djnz shr
	ret
	
GetKey:
	push hl
		push de
			push bc
			ld e,$fe			;frist group
			ld c,$01			;key port
			ld l,0				;l holds key pressed
cscloop:
			ld a,$ff			;For some reason emulator really wants it in the loop
			out (1),a			;reset keyport
			ld h,$fe
			out (c),e			;set keygroup
			ld b,8				;loop, Delay needed when work with key driver
			in a,(c)			;read key
cscbit:
			inc l				;inc to get key pressed
			rra					;if key pressed done
			jp nc,donecsc
			rlc h
			djnz cscbit			;loop 8
			rlc e				;next key group
			jp m,cscloop		;if bit 7 set loop
			ld l,0				;if no key pressed 0
donecsc:
			ld a,$ff
			out (1),a
			ld a,e
			cpl
			out (1),a
			nop
			nop
			in a,(1)
			inc a
			jp z,nootherkeypressed
			ld l,0
nootherkeypressed:
			ld a,$ff
			out (1),a
			nop
			ld a,e
			out (1),a
			nop
			nop
			in a,(1)
			cp h
			jr z,only1key
			ld l,0
only1key:
			ld a,l;
			or a
			pop bc
		pop de
	pop hl
	ret

;-----> Copy the gbuf to the screen and clear graph buffer (fast)
;Input: nothing
;Output:graph buffer is copied to the screen and subsequently cleared
fastCopy:
	di
	ld a,$80
	out ($10),a
	ld hl,plotsscreen-12-(-(12*64)+1)
	ld a,$20
	ld c,a
	inc hl
	dec hl
_
	ld b,64
	inc c
	ld de,-(12*64)+1
	out ($10),a
	add hl,de
	ld de,10
_
	add hl,de
	inc hl
	inc hl
	inc de
	ld a,(hl)
	out ($11),a
	dec de
	djnz -_
	ld a,c
	cp $2B+1
	jr nz,--_
	ret	
 
cols = 12
Drawmapsq:
		ld de,plotsscreen
		ld bc,0
dmsq:
		call find 				;find the tile
		and %01111111			;this is for collisions
		ld l,a					;load tile into hl
		ld h,0
		add hl,hl				;*2
		add hl,hl				;*4
		add hl,hl				;*8
		add hl,hl				;*16
		add hl,hl				;*32
			push bc				;save offsets
			ld bc,tiledata		;get tile offset
			add hl,bc
			ex de,hl			;hl = buffer de = tiledata
			ld b,16
_
				push bc
				ld bc,11		;go by rows? why would anyone do this...seriously
				ld a,(de)		;copy it the two bytes 16 times
				ld (hl),a
				inc de
				inc hl
				ld a,(de)
				ld (hl),a
				inc de
				add hl,bc
			pop bc
			djnz -_
	
			ex de,hl			;the start expects de to be the buffer
		pop bc					;we saved bc as our counter
		inc b					;b = b+1 this is the height
		ld a,b
		cp 4					;check if were at the end of the map
		jp nz,dmsq
		ld b,0					;were done with this column
		ld hl,-766				;need to move back to the top of the column
		add hl,de
		ex de,hl				;hl is destroyed, de points to the buffer
		ld a,c					;c = our counter for width
		inc c
		cp 5					;if its 5 and were here its all done
		jp nz,dmsq
		ret


Drawrow:
		ld de,apdbuf
		ld c,0
dmrow:
		call find
		and %01111111
		ld l,a
		ld h,0
		add hl,hl
		add hl,hl
		add hl,hl
		add hl,hl
		add hl,hl				;*32
			push bc
			ld bc,tiledata
			add hl,bc
			ex de,hl
			ld b,16
_
				push bc
				ld bc,11
				ld a,(de)
				ld (hl),a
				inc de
				inc hl
				ld a,(de)
				ld (hl),a
				add hl,bc
				inc de
			pop bc
			djnz -_
	
			ld bc,2-(cols*16)
			add hl,bc
			ex de,hl
		pop bc
		inc c
		ld a,c
		cp 6
		jp nz,dmrow
		ret

;1,2
;3,4	-->  2,4,6...1,3,5
;5,6
;...
;64 bytes seperate each section
Drawcol:
	ld de,apdbuf
	ld b,0
dmcol:
	call find			;find tile data
	and %01111111
	ld l,a
	ld h,0
	add hl,hl
	add hl,hl
	add hl,hl
	add hl,hl
	add hl,hl				;*32
	push bc
		ld bc,tiledata
		add hl,bc			;get location of tile
		ex de,hl			;de == tile data
		ld b,16				;number of bytes to copy
_
		push bc
			ld bc,64
			add hl,bc
			ld a,(de)
			ld (hl),a		;#1 loaded
			inc de
	
			ld bc,-64
			add hl,bc
			ld a,(de)
			ld (hl),a		;#2 loaded
			inc de
			inc hl
		pop bc
		djnz -_

		ex de,hl
		pop bc
	inc b
	ld a,b
	cp 4
	jp nz,dmcol
	ret

;b = offy
;c = offx
;returns hl -> tile, a contains the tile#
Find:
	push de
		ld a,(mapwidth)
		ld d,0
		ld e,a
		ld a,(offy)
		add a,b
		bit 7,a
		jr nz,loadblanktile
		push de
			ld d,a
			ld a,(mapheight)
			dec a
			cp d
			ld a,d
		pop de
		jr c,loadblanktile
		call multdebya
		ld a,(offx)
		add a,c
		bit 7,a
		jr nz,loadblanktile
		push de
			ld d,a
			ld a,(mapwidth)
			dec a
			cp d
			ld a,d
		pop de
		jr c,loadblanktile
		ld e,a
		ld d,0
		add hl,de
		ld de,mapdata+2						;accounting for the size bytes
		add hl,de
		ld a,(hl)
	pop de
	ret
LoadBlankTile:
	ei
	ld a,(blanktile)
	pop de
	ret

	;i think this is hl = de * a
MultDEbyA:
	ld hl,0
	rlca
	jp nc,$+4
	add hl,de

	add hl,hl
	rla
	jp nc,$+4
	add hl,de

	add hl,hl
	rlca
	jp nc,$+4
	add hl,de

	add hl,hl
	rlca
	jp nc,$+4
	add hl,de

	add hl,hl
	rlca
	jp nc,$+4
	add hl,de

	add hl,hl
	rlca
	jp nc,$+4
	add hl,de
	
	add hl,hl
	rlca
	jp nc,$+4
	add hl,de

	add hl,hl
	rlca
	ret nc
	add hl,de
	ret
#endcomment
